package com.gimi.cloud.bbp.manage.service.sys.impl;

import com.gimi.cloud.bbp.common.utils.NumberUtil;
import com.gimi.cloud.bbp.dao.dao.sys.SysMenuDao;
import com.gimi.cloud.bbp.dto.sys.SysMenuDTO;
import com.gimi.cloud.bbp.entity.sys.SysMenuEntity;
import com.gimi.cloud.bbp.dto.params.MenuSingleParams;
import com.gimi.cloud.bbp.manage.service.sys.MenuService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuServiceImpl implements MenuService {

    private final SysMenuDao menuDao;
    private static Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Autowired
    public MenuServiceImpl(SysMenuDao menuDao){
        this.menuDao = menuDao;
    }


    @Override
    public List<SysMenuDTO> getMenuList() {
        return menuDao.getMenuList();
    }

    @Override
    public SysMenuDTO getMenuSingle(MenuSingleParams params) {
        return menuDao.getMenuSingle(NumberUtil.getInt(params.getMenuId()));
    }

    @Override
    public List<SysMenuDTO> getMenuListByRoleId(Integer roleId) {
        return menuDao.getMenuListByRoleId(roleId);
    }

    /**
     * 通过上级id查询全部菜单
     * @param menuDTO
     * @return
     */
    @Override
    public List<SysMenuDTO> getMenuListByPid(SysMenuDTO menuDTO) {
        Integer mePid = 0;
        if(menuDTO!=null && menuDTO.getMeId()!=null){
            mePid = menuDTO.getMeId();
        }
        return menuDao.getMenuListByPid(mePid);
    }

    @Override
    public List<SysMenuDTO> getMenuTree(List<SysMenuDTO> allList, List<SysMenuDTO> authList) {
        allList = initCheck(allList,authList);
        return sortMenu(allList);
    }

    /**
     * 添加菜单
     * @param menuDTO
     * @return
     */
    @Override
    public Boolean menuAdd(SysMenuDTO menuDTO) {
        SysMenuEntity menuEntity = new SysMenuEntity();
        BeanUtils.copyProperties(menuDTO,menuEntity);
        return menuDao.insertSelective(menuEntity) > 0;
    }

    /**
     * 更新菜单
     * @param menuDTO
     * @return
     */
    @Override
    public Boolean menuUpdate(SysMenuDTO menuDTO) {
        SysMenuEntity menuEntity = new SysMenuEntity();
        BeanUtils.copyProperties(menuDTO,menuEntity);
        return menuDao.updateByPrimaryKey(menuEntity) > 0;
    }

    /**
     * 通过主键id删除菜单，同时删除相关下级菜单
     * 通过递归删除：判断当前菜单有无子菜单，存在子菜单进行递归删除，不存在直接删除
     * @param menuDTO
     * @return
     */
    @Override
    public Boolean menuDelete(SysMenuDTO menuDTO) {
        List<SysMenuDTO> menus = menuDao.getMenuListByPid(menuDTO.getMeId());
        if(null!=menus && menus.size()>0){
            for(SysMenuDTO menu : menus){
                this.menuDelete(menu);
            }
        }
        //设置删除标识
        menuDTO.setIsDelete(1);
        SysMenuEntity entity = new SysMenuEntity();
        BeanUtils.copyProperties(menuDTO,entity);
        menuDao.updateByPrimaryKey(entity);
        return true;
    }

    /**
     * 初始化是否选中
     * @param allList 总菜单
     * @param authList 选中菜单
     * @return
     */
    List<SysMenuDTO> initCheck(List<SysMenuDTO> allList, List<SysMenuDTO> authList){
        if(allList != null && allList.size() > 0 && authList != null && authList.size() > 0){
            for(SysMenuDTO outer : allList){
                for(SysMenuDTO inner : authList){
//                    if (outer.getMeId() == inner.getMePid()){
//                        outer.setCheck("1");
//                    }else{
//                        outer.setCheck("0");
//                    }
                }
            }
        }
        return allList;
    }

    @Override
    public List<SysMenuDTO> sortMenu(List<SysMenuDTO> rootMenu) {
        List<SysMenuDTO> menuList = new ArrayList<>();
        for (int i = 0; i < rootMenu.size(); i++) {
            Integer pId = rootMenu.get(i).getMePid();
            if (pId != null && pId == 0) {
                menuList.add(rootMenu.get(i));
            }
        }
        for (SysMenuDTO menu : menuList) {
            menu.setChilds(getChild(menu.getMeId(), rootMenu));
        }
        return menuList;
    }

    private List<SysMenuDTO> getChild(Integer id, List<SysMenuDTO> rootMenu) {
        // 子菜单
        List<SysMenuDTO> childList = new ArrayList<>();
        for (SysMenuDTO menu : rootMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (menu.getMePid() != null) {
                if (menu.getMePid().equals(id)) {
                    childList.add(menu);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (SysMenuDTO menu : childList) {
            if (StringUtils.isBlank(menu.getMeLink())) {
                menu.setChilds(getChild(menu.getMeId(), rootMenu));// 递归
            }
        }
        if (childList.size() == 0) {
            // 递归出口
            return null;
        }
        return childList;
    }

}
